package house.service;

import house.entity.House;
import house.entity.COrder;
import house.entity.Res;
import house.repository.HouseRepository;
import house.repository.OrderRepository;
import house.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HouseRepository houseRepository;

    public Res create(COrder order, Integer houseId, Integer userId) {
        House house = houseRepository.findOne(houseId);
        order.setHouse(house);
        order.setOwner(house.getOwner());
        order.setTenant(userRepository.findOne(userId));
        order.setStatus("待房东确认");
        order = orderRepository.save(order);
        return new Res(200, "ok", order);
    }

    public Res update(Integer orderId, String status) {
        COrder order = orderRepository.findOne(orderId);
        order.setStatus(status);
        order = orderRepository.save(order);
        return new Res(200, "ok", order);
    }

    public Res getByUserId(Integer userId) {
        return new Res(200, "ok", orderRepository.findAllByTenantIdOrderByCreateDateDesc(userId));
    }
}
